package com.xhwy.service.impl;

import com.xhwy.entity.Product;
import com.xhwy.entity.ShoppingCart;
import com.xhwy.mapper.ProductMapper;
import com.xhwy.mapper.ShoppingCartMapper;
import com.xhwy.param.ShoppingCartSaveParam;
import com.xhwy.param.UserIdParam;
import com.xhwy.service.ShoppingCartService;
import com.xhwy.util.R;
import com.xhwy.vo.ShoppingCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartSaveParam
     * @return
     */
    @Override
    public R add(ShoppingCartSaveParam shoppingCartSaveParam) {
        //查询商品数据
        Product product = productMapper.getByProductId(shoppingCartSaveParam.getProductId());
        if (product == null) return R.fail("商品已经被删除，无法添加到购物车");
        //检查库存
        if (product.getInventory() == 0) return R.fail("库存不足，无法添加");
        //检查是否添加过
        ShoppingCart shoppingCart = shoppingCartMapper.getByUserIdAndProductId(shoppingCartSaveParam);
        if(shoppingCart != null){
            //证明购物车存在该商品
            if(shoppingCart.getProductNumber() < product.getInventory()) {
                //原有数量加1
                shoppingCart.setProductNumber(shoppingCart.getProductNumber() + 1);
                //更新数据
                int rows = shoppingCartMapper.update(shoppingCart);
                return R.ok("更新数据成功");
            }else {
                return R.fail("库存不足，无法添加");
            }
        }
        //添加购物车
        shoppingCart = new ShoppingCart();
        shoppingCart.setProductNumber(1);
        shoppingCart.setUserId(shoppingCartSaveParam.getUserId());
        shoppingCart.setProductId(shoppingCartSaveParam.getProductId());
        int rows = shoppingCartMapper.add(shoppingCart);

        //结果封装和返回
        ShoppingCartVo cartVo = new ShoppingCartVo(product, shoppingCart);

        return R.ok("购物车数据添加成功",cartVo);
    }

    /**
     * 返回购物车数据
     *
     * @param userIdParam 已经校验过的用户id
     * @return
     */
    @Override
    public R getByUserId(UserIdParam userIdParam) {
        //查询用户购物车数据
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getByUserId(userIdParam.getUserId());
        //判断是否存在
        if (shoppingCarts == null || shoppingCarts.size() == 0) return R.ok("查询数据成功",new ArrayList<>());
        //获取商品数据集合
        //方式一
        List<Integer> productIds = new ArrayList<>();
        for (ShoppingCart cart : shoppingCarts) {
            productIds.add(cart.getProductId());
        }
        List<Product> products = productMapper.getByProductIdCollection(productIds);
        Map<Integer, Product> productMap = products.stream().collect(Collectors.toMap(Product::getProductId, v -> v));
        
        //进行数据封装返回
        List<ShoppingCartVo> cartVoList = new ArrayList<>();
        for (ShoppingCart cart : shoppingCarts) {
            ShoppingCartVo cartVo = new ShoppingCartVo(productMap.get(cart.getProductId()),cart);
            cartVoList.add(cartVo);
        }
        return R.ok("数据查询成功", cartVoList);
    }

    /**
     * 更新购物车业务
     * 1.查询商品数据
     * 2.判断库存是否可用
     * 3.正常修改即可
     * @param shoppingCart
     * @return
     */
    @Override
    public R update(ShoppingCart shoppingCart) {
        Product product = productMapper.getByProductId(shoppingCart.getProductId());
        if (shoppingCart.getProductNumber() > product.getInventory()) {
            return R.fail("修改失败，库存不足");
        }
        //修改数据
        ShoppingCartSaveParam saveParam = new ShoppingCartSaveParam();
//        saveParam.setUserId(shoppingCart.getUserId());
//        saveParam.setProductId(shoppingCart.getProductId());
//        ShoppingCart newCart = shoppingCartMapper.getByUserIdAndProductId(saveParam);
        int rows = shoppingCartMapper.update(shoppingCart);
        return R.ok("修改购物车数量成功");
    }

    /**
     * 删除购物车数据
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public R delete(ShoppingCart shoppingCart) {
        int rows = shoppingCartMapper.delete(shoppingCart);
        return R.ok("删除购物车成功");
    }
}
